package com.bwie.redkids;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.bwie.redkids.adapter.PromotionBaseAdapter;
import com.bwie.redkids.bean.SalesPromotionBean;
import com.bwie.redkids.bean.SalesPromotionBean.MyTopic;
import com.bwie.redkids.utils.GsonUtils;
import com.bwie.redkids.utils.PathUtils;
import com.google.gson.Gson;

public class SalesPromotionActivity extends Activity implements OnClickListener {

	protected static final int SUCESS = 0;

	private ListView promotion_list;
	private ArrayList<MyTopic> arrayList = new ArrayList<SalesPromotionBean.MyTopic>();
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == SUCESS) {
				SalesPromotionBean promotionBean = (SalesPromotionBean) msg.obj;
				arrayList.addAll(promotionBean.getTopic());
				promotion_list.setAdapter(new PromotionBaseAdapter(
						SalesPromotionActivity.this, arrayList));
			}
		};
	};

	private Button promotion_head_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_sales_promotion);
		promotion_list = (ListView) findViewById(R.id.promotion_list);
		promotion_head_back = (Button) findViewById(R.id.promotion_head_back);
		promotion_head_back.setOnClickListener(this);
		getInformation();
	}

	private void getInformation() {
		new Thread() {
			public void run() {
				try {
					String data = GsonUtils.getData(PathUtils.PATH
							+ PathUtils.PROMOTION);
					Gson gson = new Gson();
					SalesPromotionBean fromJson = gson.fromJson(data,
							SalesPromotionBean.class);
					handler.obtainMessage(SUCESS, fromJson).sendToTarget();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.promotion_head_back:
			SalesPromotionActivity.this.finish();
			break;

		default:
			break;
		}
	}
}
